package com.qf.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsTopic;
import com.qf.service.DtsTopicService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/topic")
@CrossOrigin("*")
public class TopicController {
    @Autowired
    private DtsTopicService topicService;

    //##### 专题列表
    //
    //- 接口地址 : http://localhost:8083/admin/
    // topic/list?page=1&limit=20&sort=add_time&order=desc
    @GetMapping("/list")
    public Object list(Integer id, String title,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit,
                       @RequestParam(defaultValue = "add_time") String sort,
                       @RequestParam(defaultValue = "desc") String order) {
        IPage<DtsTopic> topicIPage = topicService.queryByPage(page, limit, id, title, sort, order);
        long total = topicIPage.getTotal();
        List<DtsTopic> items = topicIPage.getRecords();
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("items", items);
        return ResponseUtil.ok(map);
    }

    //##### 专题添加
    //
    //- 接口地址 : http://localhost:8083/admin/topic/create
    @PostMapping("/create")
    public Object insertOne(@RequestBody HashMap<String,Object> map){
        DtsTopic topic = JSON.parseObject(JSON.toJSONString(map), DtsTopic.class);
        DtsTopic dtsTopic = topicService.addOne(topic);
        return ResponseUtil.ok(dtsTopic);
    }


}
